#include<iostream.h>
#include<fstream.h>

int main()
{
	fstream file;
	char fname[50];
	int rows,cols;
	int i,j;

	cout << "type File name : ";
	cin >> fname;

	file.open(fname,ios::in);
	if(!file)
	{
		cout << "error reading file";
		return (1);
	}

	
	cout << "How many rows : ";
	cin >> rows;
	cout<< "How many columns : ";
	cin >> cols;

	float **array;
	array = new float*[rows];
	
	for(i=0; i<rows; i++)
		array[i] = new float [cols];


	for(i=0; i<rows; i++)
	{
		for(j=0; j<cols; j++)
			//file.read(reinterpret_cast<char*>(&array[i][j]),sizeof(float));
			file >> array[i][j];
	}
	file.close();

	// use the data in array here
	/*for(i=0; i<rows; i++)
	{
		for(j=0; j<cols; j++)
			cout << array[i][j] << " ";
		cout <<endl;
	}*/

	float avg=0;
	int a,d,b;
	float h;
	int fact;
	char fwrite[50];
	cout << "Enter the file name to be written : ";
	cin >> fwrite;
	file.open(fwrite,ios::out);

	//file >> filename;
	//file.open(fwrite,ios::out);

	do{
	cout << "Enter A (initial row number) ";

	cin >> a;
	}while(a<1);
	cout << "Enter D (common difference in rows) ";
	cin >> d;
	cout << "Enter First Col Value ";
	cin >> h;
	
	file << h << '\t';
	fact=int(((rows-a)/d)+1);
	for(b=3;b<cols;b++)
	{

		avg=0;
		for(j=(a-1);j<rows;j+=d)
		{
		avg+=array[j][b];
		}
		avg/=fact;
		file << avg << '\t';
	}

	// free the memory and exit
	for(i=0; i<rows; i++)
		delete(array[i]);
	delete(array);

	return 0;
}
